package com.javasm.sys.controller;

import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.enums.OperatorAction;
import com.javasm.commons.enums.OperatorType;

import com.javasm.sys.DepartService;
import com.javasm.sys.annotations.MyLog;
import com.javasm.sys.SysDepart;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("dept")
public class DepartController {
    @Resource
    private DepartService ds;
    @GetMapping("getdept")
    //获取所有部门信息
    public AxiosResult getByDept(){
        List<SysDepart> alldept = ds.getAlldept();
        return AxiosResult.suc(alldept);
    }

    //获取部门树
    @GetMapping("tree")
    @MyLog(title = "部门管理",operationAction= OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult getDeptByTree(){
        List<SysDepart> trees = ds.getList();
        return AxiosResult.suc(trees);
    }

    //新增
    @PostMapping("add")
    @MyLog(title = "部门管理",operationAction= OperatorAction.INSERT,operatorType = OperatorType.MANAGE)
    public AxiosResult insertDeptData(@RequestBody SysDepart dp){
        if (ds.insertDept(dp))return AxiosResult.suc();
        return AxiosResult.error();
    }
    //删除
    @DeleteMapping("delete/{id}")
    @MyLog(title = "部门管理",operationAction= OperatorAction.DELETE,operatorType = OperatorType.MANAGE)
    public AxiosResult deleteDept(@PathVariable String id){
        if (ds.delete(id))return AxiosResult.suc();
        return AxiosResult.error();
    }

    // 根据id查询数据
    @GetMapping("byid/{id}")
    public AxiosResult dataForEdit(@PathVariable String id){
        SysDepart byId = ds.getById(id);
        return AxiosResult.suc(byId);
    }
    // 修改数据
    @PutMapping("edit")
    @MyLog(title = "部门管理",operationAction= OperatorAction.UPDATE,operatorType = OperatorType.MANAGE)
    public AxiosResult updateDept(@RequestBody SysDepart id){
        if (ds.update(id))return AxiosResult.suc();
        else return AxiosResult.error();
    }
    //分级
    @GetMapping("mhfy")
    public AxiosResult getByNameAndStu(SysDepart d){
        List<SysDepart> byNameAndStu = ds.getByNameAndStu(d);
        return AxiosResult.suc(byNameAndStu);
    }
}
